package cn.yunhe.service.impl;

import cn.yunhe.dao.IEmpDao;
import cn.yunhe.dao.impl.EmpDao;
import cn.yunhe.eneity.Emp;
import cn.yunhe.service.IEmpService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Getter
@Setter
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true,rollbackFor = RuntimeException.class)
public class EmpService implements IEmpService {
    @Resource
    private EmpDao empDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true,rollbackFor =RuntimeException.class)
    public void addEmp(Emp emp) {
        empDao.addEmp(emp);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true,rollbackFor =RuntimeException.class)
    public void addEmp(List<Emp> list) {
        try {
            for (Emp e : list
            ) {
                empDao.addEmp(e);
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true,rollbackFor =RuntimeException.class)
    public void delEmp(int empno) {
        empDao.DelEmp(empno);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true,rollbackFor =RuntimeException.class)
    public void updateEmp(Emp emp) {
        empDao.updateEmp(emp);
    }

    @Override
    public Emp get(int empno) {
        return empDao.get(empno);
    }

    @Override
    public List<Emp> searchEmp(Emp emp) {
        return empDao.searchEmp(emp);
    }

    @Override
    public List<Emp> searchEmp(String hql) {
        return empDao.searchEmp(hql);
    }
}
